import Taro, {Component} from "@tarojs/taro";
import {View, Image, Text} from "@tarojs/components";
import {toJS} from 'mobx'
import {inject, observer} from '@tarojs/mobx'
import {IStoreProps} from '../../store/Stores'
import {getRuntime, pageSize, subtime} from '../../utils/common'

import './index.less'
import none_people from "../../assets/img/none_people.png";

// 我的申请
interface IHomeProps extends IStoreProps {}

@inject("stores")
@observer
class ApplyLog extends Component<IHomeProps> {
  constructor(props) {
    super(props)
  }

  state = {
    pageIndex:1,
    pageSize,
    items: [],
    totalCount: 0,
  }

  componentWillMount(): void {
    Taro.setNavigationBarTitle({ title:'我的申请' })
    this.getMyApplication()
  }

  handelRouter(url: string = '/pages/index/index'): void {
    getRuntime().navigateTo(url)
  }

  getMyApplication() {
    let { userStore } = this.props.stores
    let { pageIndex,pageSize } = this.state
    userStore.getMyApplication({
      pageIndex,
      pageSize,
      callback: res => {
        if (pageIndex === 1) {
          this.setState({totalCount: toJS(res.totalCount), items: toJS(res.items)})
        }else {
          const { items } = this.state;
          items.push(...res.items || []);
          this.setState({totalCount: res.totalCount, items})
        }
      }
    })
  }

  onReachBottom(): void {
    let {totalCount, pageSize, pageIndex} = this.state
    const {loading} = this.props.stores.homeStore;
    if (totalCount > pageIndex * pageSize && !loading) {
      this.setState({
        pageIndex: pageIndex + 1
      }, () => {
        this.getMyApplication()
      })
    }
  }

  render() {
    let { items } = this.state
    return (
      <View className='my-my_integral'>
        {items.length>0&&
        items.map(item => {
          return(
            <View className='list-19-item'>
              <View className='l'>
                <View className='list-title'>{item.typeName}</View>
                <View className='list-info'>{subtime(item.creationTime)}</View>
              </View>
              {item.status===1&&
              <View className='r apply-res'>
                 <Text className='res'>处理中...</Text>
              </View>
              }
              {item.status === 2 &&
              <View className='r apply-res'>
                 <Text className='res'>办理成功</Text>
              </View>
              }
              {item.status === 3 &&
              <View className='r apply-res '>
                 <Text className='res color'>办理失败</Text>
                 <Text className='desc color'>原因：{item.reason}</Text>
              </View>
              }
            </View>
          )
        })
        }

        {items.length<=0&&
        <View className='none-info'>
           <Image className='none-icon' src={none_people} />
           <Text className='none-title list-desc'>您还没有任何申信息~</Text>
        </View>
        }
      </View>
    )
  }
}

export default ApplyLog
